﻿using System;
using System.Collections.Generic;
using System.Text;
using T.FAS.Runtime.Base.Entity;
using T.FAS.Runtime.Metadata.Client;
using T.FAS.Runtime.Metadata.Entity;

namespace T.FAS.Runtime.Action.ExpressionPlugin
{
    public class RefSchemaExprAPI
    {
        private static readonly object synObj = new object();

        private static RefSchemaExprAPI _instance = null;
        public bool IsDev { get; set; }
        public static RefSchemaExprAPI GetInstance
        {
            get
            {
                if (_instance == null)
                {
                    lock (synObj)
                    {
                        if (_instance == null)
                            _instance = new RefSchemaExprAPI();
                    }
                }
                return _instance;
            }
        }
        private RefSchemaExprAPI()
        { }
        public string GetExpr(string exprContent, bool isNeedAddNewLayer = false)
        {
            if (isNeedAddNewLayer)
                return $"${{{exprContent}}}";
            else
                return exprContent;
        }

        /// <summary>
        /// 获取路径表达式值
        /// </summary>
        /// <param name="metaData"></param>
        /// <param name="exprContent"></param>
        /// <returns></returns>
        public string GetExprValue(IMetaDataContent metaData, string exprContent, bool isLeaf)
        {
            if (!IsDev)
                return "";
            try
            {
                if (metaData == null)
                {
                    return null;
                    //throw new ArgumentNullException(nameof(metaData));
                }
                if (string.IsNullOrEmpty(exprContent))
                    throw new ArgumentNullException(nameof(exprContent));
                if (isLeaf)
                {
                    if (exprContent.StartsWith("${${"))
                        throw new Exception($"表达式项Expr解析层级出错");
                    var arr = exprContent.Split('.');
                    if (arr.Length <= 3)
                        return null;
                    /*ToDo
                    var exprValue = MetaDataDevClientService.GetInstance().GetValueFromSchema(metaData.MetaDataID, metaData.ProjectBranchID, exprContent);
                    return exprValue;
                    */
                }
                return null;
            }
            catch (Exception e)
            {
                throw new Exception($"获取表达式value出错", e);
            }
        }

        /// <summary>
        /// 获取路径表达式值
        /// </summary>
        /// <param name="metaData"></param>
        /// <param name="exprContent"></param>
        /// <returns></returns>
        public string GetExprValue(IMetaData metaData, string exprContent, bool isLeaf, IRefSchemaEntity refSchemaEntity = null)
        {
            if (!IsDev)
                return "";
            try
            {
                if (metaData == null)
                {
                    if (DebugControl.EnableDebug)
                        throw new Exception($"metaData不能为空");
                    else
                        return null;
                }
                if (string.IsNullOrEmpty(exprContent))
                    throw new ArgumentNullException(nameof(exprContent));
                if (isLeaf)
                {
                    if (exprContent.StartsWith("${${"))
                        throw new Exception($"表达式项Expr解析层级出错");
                    var arr = exprContent.Split('.');
                    if (arr.Length <= 3)
                        return null;
                    /* Todo:
                    var exprValue = MetaDataDevClientService.GetInstance().GetValueFromSchema(metaData, exprContent, refSchemaEntity);
                    return exprValue;
                    */
                }
                return null;
            }
            catch (Exception e)
            {
                throw new Exception($"获取表达式value出错", e);
            }
        }

        /// <summary>
        /// 获取路径表达式值
        /// </summary>
        /// <param name="refSchemaEntity"></param>
        /// <param name="exprContent"></param>
        /// <returns></returns>
        public string GetExprValue(IRefSchemaEntity refSchemaEntity, string exprContent, bool isLeaf)
        {
            if (!IsDev)
                return "";
            try
            {
                if (refSchemaEntity == null)
                {
                    if (DebugControl.EnableDebug)
                        throw new Exception($"refSchemaEntity不能为空");
                    else
                        return null;
                }
                if (string.IsNullOrEmpty(exprContent))
                    throw new ArgumentNullException(nameof(exprContent));
                if (isLeaf)
                {
                    if (exprContent.StartsWith("${${"))
                        throw new Exception($"表达式项Expr解析层级出错");
                    var arr = exprContent.Split('.');
                    if (arr.Length <= 3)
                        return null;
                    /* Todo:
                    var exprValue = MetaDataDevClientService.GetInstance().GetValueFromRefSchemaEntity(refSchemaEntity, exprContent);
                    return exprValue;
                    */
                }
                return null;
            }
            catch (Exception e)
            {
                throw new Exception($"获取表达式value出错", e);
            }
        }

        public IRefSchemaEntity GetRefSchemaEntity(IMetaDataContent metaDataContent)
        {
            if (!IsDev)
                return null;
            if (metaDataContent == null)
            {
                if (DebugControl.EnableDebug)
                    throw new Exception($"metaDataContent不能为空");
                else
                    return null;
            }

            /* Todo:
            return MetaDataDevClientService.GetInstance().GetRefSchemaEntity(metaDataContent);
            */
            return null;
        }
    }
}
